/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.asset.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import top.continew.admin.asset.model.query.CheckItemQuery;
import top.continew.admin.asset.model.req.CheckItemReq;
import top.continew.admin.asset.model.resp.CheckItemResp;
import top.continew.admin.asset.service.CheckItemService;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.asset.model.query.CheckTaskQuery;
import top.continew.admin.asset.model.req.CheckTaskReq;
import top.continew.admin.asset.model.resp.CheckTaskDetailResp;
import top.continew.admin.asset.model.resp.CheckTaskResp;
import top.continew.admin.asset.service.CheckTaskService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.BasePageResp;

/**
 * 资产管理-盘点-任务 API
 *
 * @author CharlieYan
 * @since 2025/07/11 15:07
 */
@Tag(name = "资产管理-盘点-任务 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/asset/check/task", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE,
    Api.EXPORT})
public class CheckTaskController extends BaseController<CheckTaskService, CheckTaskResp, CheckTaskDetailResp, CheckTaskQuery, CheckTaskReq> {

    private final CheckTaskService checkTaskService;
    private final CheckItemService checkItemService;

    @Operation(summary = "取消盘点", description = "盘点-取消盘点")
    @SaCheckPermission("asset:check:task:cancel")
    @GetMapping(value = "/cancel/{id}")
    public void cancel(@PathVariable Long id) {
        checkTaskService.updateCheckTaskStatus(id, 1);
    }

    @Operation(summary = "盘点清单", description = "盘点清单")
    @SaCheckPermission("asset:check:task:items")
    @GetMapping(value = "/items")
    public BasePageResp<CheckItemResp> items(CheckItemQuery query, PageQuery pageQuery) {
        return checkItemService.page(query, pageQuery);
    }

    @Operation(summary = "盘点处理", description = "盘点处理")
    @SaCheckPermission("asset:check:task:items:check")
    @PutMapping(value = "/items/check")
    public void itemsCheck(CheckItemReq req, Long id) {
        checkItemService.update(req, id);
    }

    @Operation(summary = "盘点清单导出", description = "盘点清单导出")
    @SaCheckPermission("asset:check:task:items:export")
    @GetMapping(value = "/items/export")
    public void export(CheckItemQuery query, SortQuery sortQuery, HttpServletResponse response) {
        checkItemService.export(query, sortQuery, response);
    }
}